package Web;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author wuhan
 * @date 2019/10/17 10:33
 */
class UdpSend2{
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line=bufr.readLine())!=null){
            if ("886".equals(line))
                break;
            byte[] buf = line.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("192.168.210.1"),10020);
            ds.send(dp);
        }
        ds.close();
    }
}



public class UdpDemo2 {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10020);
        while (true){
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            int port = ds.getPort();
            InetAddress inetAddress = dp.getAddress();
            String hostAddress = inetAddress.getHostAddress();
            String string = new String(dp.getData(),0,dp.getLength());
            System.out.println("port="+port+" "+"hostAddress="+hostAddress+"  data="+string);
        }
    }
}
